package com.ctrl.riveradministrator.mvp.view.activity.login;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.Login;
import com.ctrl.riveradministrator.mvp.presenter.LoginPresenter;
import com.ctrl.riveradministrator.MainActivity;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginView {

    private Button loginBtn;
    private EditText usernameEt, passwordEt;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(View view) {
        usernameEt = $(R.id.username_et);
        passwordEt = $(R.id.password_et);
        loginBtn = $(R.id.login_btn);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("登录中...");
        pDialog.setCancelable(false);
    }

    @Override
    protected void doBusiness(Context mContext) {

    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                if (usernameEt.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入账号...", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (passwordEt.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入密码...", Toast.LENGTH_SHORT).show();
                    return;
                }
                mPresenter.login(usernameEt.getText().toString(), passwordEt.getText().toString());
                break;
        }
    }

    @Override
    protected void setListener() {
        loginBtn.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void showLoading() {
        super.showLoading();
        pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        pDialog.cancel();
    }

    @Override
    public void onSuccess(Login login) {
        Log.i("cyc", "login===>" + login);

        if (login!=null){
            RaApplication.editor.putString(Constants.USER_ID, String.valueOf(login.getUserId()));
            RaApplication.editor.putString(Constants.USER_PHONE, login.getPhonenumber());
            RaApplication.editor.putString(Constants.USER_DEPARTMENT, login.getDept().getDeptName());
            RaApplication.editor.putString(Constants.USER_ROLE, login.getRemark());
            RaApplication.editor.putString(Constants.USER_NAME, login.getUserName());
            //RaApplication.editor.putString(Constants.USER_RIVER,login.getPhonenumber());
            //RaApplication.editor.putString(Constants.USER_SERVER,login.getPhonenumber());
            RaApplication.editor.apply();
        }
        startActivity(MainActivity.class);
        finish();
    }

    @Override
    public void onFailed(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
